package com.aztec.ims.service.dao;

import java.util.Iterator;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Session;

import com.aztec.ims.domain.Address;
import com.aztec.ims.service.om.Carriers;
import com.aztec.ims.service.om.PaymentTerms;
import com.aztec.ims.service.om.TaxingScheme;
import com.aztec.ims.service.om.VendorAddress;
import com.aztec.ims.service.om.VendorDetails;
import com.aztec.ims.service.request.CreateVendorRequest;
import com.aztec.ims.service.response.CreateVendorResponse;
import com.aztec.ims.service.util.HibernateUtil;

public class VendorDetailsDao {
	public CreateVendorResponse createVendor(
			CreateVendorRequest createVendorRequest) {
		VendorDetails om = convertRequestToOm(createVendorRequest);

		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
    	session.save(om);
		session.getTransaction().commit();
        
		//Save the custom informtion
		CustomInfoDao customInfoDao = new CustomInfoDao();
    	customInfoDao.createCustomInfo(createVendorRequest.getCustomInfo(), 'V', om.getVendorId().toString());
		
		CreateVendorResponse response = new CreateVendorResponse();
		response.setVendorId(""+om.getVendorId());
		return response;
		
	}

	private VendorDetails convertRequestToOm(
			CreateVendorRequest request) {

		VendorDetails om = new VendorDetails();
		om.setVendorName(request.getVendorName());
		om.setContactName(request.getVendorCOntact().getContactName());
		om.setContactPhone(request.getVendorCOntact().getContactPhone());
		om.setContactFax(request.getVendorCOntact().getContactFax());
		om.setContactEmail(request.getVendorCOntact().getContactEmail());
		om.setContactWebsite(request.getVendorCOntact().getContactWebsite());
        om.setRemarks(request.getRemarks());
        
        if(request.isActiveFlag()){
        	om.setActiveFlag(new Character('Y'));
        }else{
        	om.setActiveFlag(new Character('N'));
        }
        
        if(null != request.getVendorAddress()){
        	for(Iterator iter = request.getVendorAddress().iterator();iter.hasNext();){
        		Address vendorAddress = (Address)iter.next();
        		VendorAddress vendorAddressOm = convertVendorAddress(vendorAddress);  
        		vendorAddressOm.setVendorDetails(om);
        		om.getVendorAddresses().add(vendorAddressOm);
        	}
        		
        }
        
        if(!StringUtils.isEmpty(request.getCarrier())){
        	Carriers carrierOm = new Carriers();
        	carrierOm.setCarrierId(new Integer(request.getCarrier()));
        	carrierOm.getVendorDetailses().add(om);
        	om.setCarriers(carrierOm);
        }
        if(!StringUtils.isEmpty(request.getPaymentTerms())){
        	PaymentTerms pymtTermsOm = new PaymentTerms();
        	pymtTermsOm.setPymtId(new Integer(request.getPaymentTerms()));
        	pymtTermsOm.getVendorDetailses().add(om);
        	om.setPaymentTerms(pymtTermsOm);
        }
        if(!StringUtils.isEmpty(request.getTaxingScheme())){
        	TaxingScheme taxingSchemeOm = new TaxingScheme();
        	taxingSchemeOm.setTaxschemeId(new Integer(request.getTaxingScheme()));
        	taxingSchemeOm.getVendorDetailses().add(om);
        	om.setTaxingScheme(taxingSchemeOm);
        }
        
		return om;
	}

	private VendorAddress convertVendorAddress(Address vendorAddress) {
		
		VendorAddress vendorAddressOm = new VendorAddress();
		vendorAddressOm.setAddrName(vendorAddress.getAddressName());
		if(vendorAddress.getAddressType() != null){
			Character addressType = new Character(vendorAddress.getAddressType().charAt(0));
			vendorAddressOm.setAddrType(addressType);	
		}
		vendorAddressOm.setCity(vendorAddress.getCity());
		vendorAddressOm.setCountry(vendorAddress.getCountry());
		vendorAddressOm.setRemarks(vendorAddress.getRemarks());
		vendorAddressOm.setState(vendorAddress.getState());
		vendorAddressOm.setStreet(vendorAddress.getStreet());
		vendorAddressOm.setZipCode(new Integer(vendorAddress.getZipCode()));
		return vendorAddressOm;
	}
	}


